package com.kusiri.kframework.salesforce.util
{
	import mx.formatters.DateFormatter;
	
	public class SfUtil
	{
		
		public function SfUtil()
		{
		}
		
		/**
		 * Just handling easy strings for now
		 */
		public static function processSfFormula(formula:String) : String
		{
			if (!formula) return null;
			var re:RegExp = new RegExp('^"([^"]*)"$');
			var o:Object = re.exec(formula);
			return o ? o[1] : null;
		}

		public static function toInt(element:*, index:int, arr:Array):void 
		{
			// type casting doesn't work
            arr[index] = parseInt(arr[index]);
        }

		public static function dateStringToDate(str:String) : Date
		{
			if (!str)
			{
				return null;
			}
			
			var arr:Array = str.split('-');
			arr.forEach(toInt);
			
			// Zero month indexing? AS bug? Wierd...
			return new Date(arr[0],arr[1]-1,arr[2]);
		}
		
		public static function dateTimeStringToDate(str:String) : Date
		{
			if (!str)
			{
				return null;
			}
			
			var arr:Array = str.split('T');
			var date:Array = arr[0].split('-');
			date.forEach(toInt);
			arr = arr[1].split('.');
			var time:Array = arr[0].split(':');
			time.forEach(toInt);
			
			// Zero month indexing? AS bug? Wierd...
			return new Date(date[0],date[1]-1,date[2],time[0],time[1],time[2]);
		}
		
		public static function dateToDateString(date:Date) : String
		{
			if (!date)
			{
				return null;
			}
			
			// why does 4 == April? Zero month indexing? AS bug? Wierd...
			var month:int = date.getMonth()+1;
			var day:int = date.getDate();
			
			return date.getFullYear()+'-'+(month < 10 ? '0' : '')+month+'-'+(day < 10 ? '0' : '')+day;
		}
		


	}
}